package com.songcha.library_business.ui.dialog

import android.content.Context
import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.compose.ui.unit.Constraints
import androidx.constraintlayout.widget.ConstraintLayout
import com.songcha.library_common.ui.dialog.CustomDialog
import com.songcha.library_common.util.dp2px
import com.songcha.library_business.R
import com.songcha.library_common.helper.ViewHelper
import com.songcha.library_common.ui.view.CustomButton

class BusinessCommonDialog(context: Context, val mTitle:String = "", val mContent:String = "",
                           val mBtnCancelText:String = "取消", val mBtnConfirmText:String = "确定",
                           val mBtnCancelStartColor:Int? = null, val mBtnCancelEndColor:Int? = null,
                           val mBtnConfirmStartColor:Int? = null, val mBtnConfirmEndColor:Int? = null,
                           val mOnCancelBlock:(()->Unit)? = null, val mOnConfirmBlock:(()->Unit)? = null)
    :CustomDialog(context) {
    init {
        custom(10f.dp2px())
        setWidth(280.dp2px())
        setBackgroundColor(Color.WHITE)
    }

    override fun hasView(): Boolean {
        return true
    }

    override fun getView(root: ViewGroup): View? {
        val view = layoutInflater.inflate(R.layout.business_dialog_common,root,false)
        val tvTitle = view.findViewById<TextView>(R.id.business_tv_title)
        val tvContent = view.findViewById<TextView>(R.id.business_tv_content)
        val btnCancel = view.findViewById<CustomButton>(R.id.business_btn_cancel)
        val btnConfirm = view.findViewById<CustomButton>(R.id.business_btn_confirm)

        ViewHelper.setVisibility(mTitle.isNotBlank(),tvTitle)
        ViewHelper.setVisibility(mContent.isNotBlank(),tvContent)

        tvTitle.text = mTitle
        tvContent.text = mContent
        btnCancel.text = mBtnCancelText
        btnConfirm.text = mBtnConfirmText

        if(mBtnCancelStartColor != null && mBtnCancelEndColor != null){
            btnCancel.setStartColor(mBtnCancelStartColor)
            btnCancel.setEndColor(mBtnCancelEndColor)
        }
        if(mBtnConfirmStartColor != null && mBtnConfirmEndColor != null){
            btnConfirm.setStartColor(mBtnConfirmStartColor)
            btnConfirm.setEndColor(mBtnConfirmEndColor)
        }

        if(mBtnCancelText.isEmpty()){
            ViewHelper.setVisibility(false,btnCancel)
            val lp = btnConfirm.layoutParams as ConstraintLayout.LayoutParams
            lp.leftToLeft = ConstraintLayout.LayoutParams.PARENT_ID
            lp.rightToRight = ConstraintLayout.LayoutParams.PARENT_ID
            lp.rightMargin = 0
            btnConfirm.layoutParams = lp
        }

        btnCancel.setOnClickListener {
            dismiss()
            if(mOnCancelBlock != null)
                mOnCancelBlock!!()
        }
        btnConfirm.setOnClickListener {
            dismiss()
            if(mOnConfirmBlock != null)
                mOnConfirmBlock!!()
        }
        return view
    }
}